package com.syqc.monitor.ui.historyline

import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import com.github.mikephil.charting.components.LimitLine
import com.github.mikephil.charting.data.Entry
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.utils.Language
import com.syqc.entity.HistoryLine
import com.syqc.monitor.R
import com.syqc.monitor.chart.MPChartUtils
import com.syqc.monitor.databinding.FragmentWholeBinding
import com.syqc.utils.Units

/**
 *@Author Jasper
 *@Time   2020/8/12
 *@Desc   整体数据展示
 */

class WholeFragment : LazyFragment<FragmentWholeBinding>(FragmentWholeBinding::inflate) {

    private val mActivityVM by activityViewModels<LineInfoViewModel>()
    override fun initView(view: View) {
        binding.tvStartTime.isSelected = Language.isEnglish()
        binding.tvEndTime.isSelected = Language.isEnglish()
    }

    override fun initData() {
        MPChartUtils.configChart(binding.speedLineChart)
        mActivityVM.speed.observe(this) {
            val limit = LimitLine(it, "${getString(R.string.desc_history_speed_threshold)} $it ${Units.speed}")
            limit.lineWidth = 2f
            limit.textSize = 10f
            binding.speedLineChart.axisLeft.addLimitLine(limit)
        }
        mActivityVM.data.observe(this) {
            it?.countData?.run {
                binding.tvWholeRunTime.text = moveLong
                binding.tvWholeStopTime.text = stopLong
                binding.tvWholeRunMile.text =
                    String.format("%s %s", mile, getString(R.string.unit_mile))
                binding.tvWholeMaxSpeed.text =
                    String.format("%s %s", maxSpeed, getString(R.string.unit_speed))
                binding.tvWholeAverSpeed.text =
                    String.format("%s %s", avgSpeed, getString(R.string.unit_speed))
                if (totalLong == null) {
                    binding.llTotalTime.isVisible = false
                    binding.llRunAvg.isVisible = false
                } else {
                    binding.tvTotalTime.text = totalLong
                    binding.tvRunAvg.text =
                        String.format("%s %s", avgMove, getString(R.string.unit_speed))
                }
            }
            binding.tvStartTime.text = "${getString(R.string.desc_start)}:${it.locations[0].time}"
            binding.tvEndTime.text = "${getString(R.string.desc_end)}:${it.locations.last().time}"
            binding.tvWholeLocatPoint.text = "${it.locations.size} ${getString(R.string.desc_unit_num)}"

            binding.tvWholeStartAddr.text = it.locations[0].addr
            binding.tvWholeEndAddr.text = it.locations.last().addr



            handlerSpeed(it.locations)
        }


    }

    override fun initEvent() {

    }

//    private fun initChart(list: List<HistoryLine.Location>?) {
//        if (list == null) return
//        val entities = ArrayList<Entry>()
//        for ((index, entity) in list.withIndex()) {
//            entities.add(Entry(index.toFloat(), entity.speed.toFloat()))
//        }
//
//        binding.speedLineChart.data =
//            LineData(MPChartUtils.getLineData(entities, Color.parseColor("#2461f7")))
//    }

    /**
     * 速度数据处理
     *
     * @param list
     */
    private fun handlerSpeed(list: List<HistoryLine.Location>) {
        val timeData = ArrayList<String>()
        val speedData = ArrayList<Entry>()
        list.forEachIndexed { index, location ->
            speedData.add(Entry(index.toFloat(), location.speed.toFloat()))
            timeData.add(location.time.substring(11))
        }
        MPChartUtils.initLineChart(binding.speedLineChart)
        MPChartUtils.initXAxis(binding.speedLineChart, timeData)
        MPChartUtils.setSpeedChartMarker(binding.speedLineChart, list)
        MPChartUtils.changeLineData(binding.speedLineChart, speedData)
    }
}